package invoker;

import invoker.Ihm;
import invoker.IhmImpl;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;


import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import command.Command;

import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class IhmImpl implements Ihm, Observer{

	private JFrame miniEditeurV1;

	private HashMap<String, Command> _lesCommandes;

	private int _debutSelection;

	private int _finSelection;

	private String _textInserer = new String();

	private JTextArea textAreaInserer;

	private JTextArea textAreaContenu;

	private JTextField textFieldSelection;

	private JPanel panelBoutons;

	private JScrollPane scrollPaneInserer;

	private JScrollPane scrollPaneContenuBuffer;

	private JButton btnInserer;

	private JButton btnCouper;

	private JButton btnColler;

	private JButton btnCopier;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IhmImpl window = new IhmImpl();
					window.miniEditeurV1.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public IhmImpl() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		miniEditeurV1 = new JFrame();
		miniEditeurV1.setTitle("Mini éditeur V1");
		miniEditeurV1.setBounds(100, 100, 450, 300);
		miniEditeurV1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		panelBoutons = new JPanel();

		scrollPaneInserer = new JScrollPane();

		scrollPaneContenuBuffer = new JScrollPane();

		textFieldSelection = new JTextField();
		textFieldSelection.setAlignmentX(Component.LEFT_ALIGNMENT);
		textFieldSelection.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(miniEditeurV1.getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(textFieldSelection, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
								.addComponent(scrollPaneContenuBuffer, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
								.addComponent(scrollPaneInserer, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
								.addComponent(panelBoutons, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE))
								.addContainerGap())
				);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(panelBoutons, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addGap(18)
						.addComponent(scrollPaneInserer, GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
						.addGap(20)
						.addComponent(scrollPaneContenuBuffer, GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(textFieldSelection, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(4))
				);

		textAreaContenu = new JTextArea();
		textAreaContenu.setEditable(false);
		textAreaContenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				_debutSelection = textAreaContenu.getSelectionStart();
				_finSelection = textAreaContenu.getSelectionEnd();
				_lesCommandes.get("Selectionner").execute();
				
				String t = "Début : "+_debutSelection+"  Fin : "+_finSelection;
				textFieldSelection.setText(t);
			}
		});
		scrollPaneContenuBuffer.setViewportView(textAreaContenu);

		textAreaInserer = new JTextArea("");
		scrollPaneInserer.setViewportView(textAreaInserer);

		btnInserer = new JButton("Inserer");
		btnInserer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				_textInserer = textAreaInserer.getText();
				_lesCommandes.get("Saisir").execute();
			}
		});
		panelBoutons.add(btnInserer);

		btnCouper = new JButton("Couper");
		btnCouper.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(_debutSelection <_finSelection)
					_lesCommandes.get("Couper").execute();
			}
		});
		panelBoutons.add(btnCouper);

		btnColler = new JButton("Coller");
		btnColler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				_lesCommandes.get("Coller").execute();
			}
		});
		panelBoutons.add(btnColler);

		btnCopier = new JButton("Copier");
		btnCopier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(_debutSelection <_finSelection)
					_lesCommandes.get("Copier").execute();
			}
		});
		panelBoutons.add(btnCopier);
		miniEditeurV1.getContentPane().setLayout(groupLayout);
		miniEditeurV1.setVisible(true);
	}

	public HashMap<String, Command> getLesCommandes() {
		return _lesCommandes;
	}

	public void setLesCommandes(HashMap<String, Command> lesCommandes) {
		this._lesCommandes = lesCommandes;
	}

	@Override
	public void update(Observable o, Object arg) {
		textAreaContenu.setText((String) arg);
		textAreaInserer.setText("");
	}

	@Override
	public String getTexteInserer() {
		return this._textInserer;
	}

	@Override
	public int getDebutSelection() {
		return _debutSelection;
	}

	@Override
	public int getFinSelection() {
		return _finSelection;
	}
}
